package com.zhuwhao.sample.utils;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhuwhao.sample.R;
import com.zhuwhao.sample.widget.DropDownMenu;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuwh on 2016/12/23 0023.
 * DropDownMenuUtils
 */
public class DropDownMenuUtils implements Serializable {

    private Context context;

    private DropDownMenu dropDownMenu;

    private OnClickListener listener;

    private List<View> popupViews = new ArrayList<>();

    public List<View> getPopupViews() {
        return popupViews;
    }

    public static class Builder {

        private DropDownMenuUtils utils;

        private int tabPosition = -1;

        public Builder(Context context, DropDownMenu dropDownMenu) {
            utils = new DropDownMenuUtils();
            utils.context = context;
            utils.dropDownMenu = dropDownMenu;
        }

        public Builder addTestView() {
            tabPosition++;
            utils.getPopupViews().add(utils.getTestView(tabPosition));
            return this;
        }

        public Builder addFilterView() {
            tabPosition++;
            utils.getPopupViews().add(utils.getFilterView(tabPosition));
            return this;
        }

        public Builder setListener(OnClickListener listener) {
            utils.listener = listener;
            return this;
        }

        public DropDownMenuUtils builder() {
            return utils;
        }
    }

    private View getTestView(final int tabPosition) {
        LinearLayout layout = new LinearLayout(context);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundResource(android.R.color.white);
        layout.setClickable(true);

        TextView textView = new TextView(context);
        textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(context, 250)));
        textView.setText("这儿什么都没有");
        textView.setTextSize(14);
        textView.setGravity(Gravity.CENTER);
        textView.setClickable(true);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dropDownMenu.close();
                listener.onClick(tabPosition, "嘿嘿嘿");
            }
        });
        layout.addView(textView);

        return layout;
    }

    private View getFilterView(final int tabPosition) {
        int padding = DensityUtils.dp2px(context, 16);

        LinearLayout layout = new LinearLayout(context);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundResource(android.R.color.white);
        layout.setPadding(padding, padding, padding, padding);
        layout.setClickable(true);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(tabPosition, "呵呵哒");
            }
        });

        EditText editText = new EditText(context);
        editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(context, 88)));
        editText.setGravity(Gravity.TOP);
        editText.setTextSize(14);
        editText.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryText));
        editText.setHintTextColor(ContextCompat.getColor(context, R.color.colorSecondaryText));
        layout.addView(editText);

        return layout;
    }

    public interface OnClickListener {
        void onClick(int tabPosition, String... tag);
    }
}
